package com.wayne.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Component("connectionPool")
public class ConnectionPool {
  private ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
  @Autowired
  private DataSource dataSource;

  public Connection getCurrentConnection() throws SQLException {
    Connection connection = threadLocal.get();
    if (connection == null) {
      connection = dataSource.getConnection();
      threadLocal.set(connection);
    }
    return connection;
  }
}
